import tools from '../dll/libs/common/tools'
const __DEV__ = true; // 调试模式
// 根据接口名和参数生成key
const pGenerateKey = (name, params) => encodeURI(name + JSON.stringify(params))

// 当前time是否为交易时间
const pIsTradeTime = time => {
  const now = tools.DateFormat(time, 'HH:mm');
  return (now >= '09:30' && now <= '11:30') || (now >= '13:00' && now <= '15:00')
}

// 合并数据
const pMergeData = (target, source) => {
  for (let i in source) {
    if (Array.isArray(source[i])) {
      target[i] = target[i].concat(source[i]);
    } else if (typeof source[i] == 'object') {
      pMergeData(target[i], source[i]);
    } else {
      target[i] = source[i];
    }
  }

  return target;
}

// 检查缓存,返回缓存最后一条数据的时间值
const pCheckBuffer = (name, params, buffer) => {
  const id = pGenerateKey(name, params),
    res = JSON.parse(JSON.stringify(params));

  if (buffer.id && buffer.id == id && buffer.time) {
    res.startTime = buffer.time;
  } else {
    buffer = {};
  }

  return res;
};

// 更新缓存
const pUpdateBuffer = (name, params, buffer, data) => {
  const id = pGenerateKey(name, params);

  if (buffer.id && buffer.id == id && data.xTime.length > 0) {
    buffer.time = data.xTime[data.xTime.length - 1];
    pMergeData(buffer.data, data)

    if (name == 'getCurrentLargeOrderFund') {
      buffer.data.stocks = data.stocks;
    }

    return buffer.data;
  } else {
    buffer.id = id;
    buffer.time = data.xTime[data.xTime.length - 1];
    buffer.data = data;

    return data;
  }
}

const DiffInterfaceArr = [
  // 'getCurrentTrends',
  // 'getRapidChange'
]

class Cache {
  static getInstance() { // 单例
    if (!Cache._instance) {
      Cache._instance = new Cache();
    }
    return Cache._instance;
  }

  // buffer = {};

  // 检查缓存
  checkBuffer (name, params) {
    if (DiffInterfaceArr.indexOf(name) < 0) {
      return params;
    }

    return pCheckBuffer(name, params, this.buffer);
  }

  // 更新缓存
  updateBuffer (name, params, data) {
    if (DiffInterfaceArr.indexOf(name) < 0) {
      return data;
    }

    return pUpdateBuffer(name, params, this.buffer, data);
  }
}

// 循环调用方法
class Loop {
  static getInstance() { // 单例
    if (!Loop._instance) {
      Loop._instance = new Loop();
    }
    return Loop._instance;
  }

  // timer = null;

  // handlers = [];

  // init () {
  //   this.clock();
  // }

  // 注册需要循环执行的方法
  register (...handlers) {
    handlers.forEach(item => {
      if (this.handlers.indexOf(item) == -1) {
        this.handlers.push(item);
      }
      this.clock();
      item();
    });
  }

  // 轮回
  clock () {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }

    this.timer = setInterval(() => {
      if (!pIsTradeTime(new Date()) && __DEV__) {
        return;
      }
      this.handlers.forEach(item => item());
    }, 60000)
  }

  // 注销循环执行的方法
  unregister (...handlers) {
    handlers.forEach(item => {
      this.handlers.indexOf(item) && this.handlers.splice(this.handlers.indexOf(item))
    })
  }

  clear () {
    clearInterval(this.timer);
    this.timer = null;
    this.handlers = [];
  }
}

export {
  Cache,
  Loop
}
